import { CENTEREVENTS } from '@amcax/base';
import { ctx2 } from '@app-base/module';
import { Vector3 } from 'three';
import Base, { createPoint, removePoint } from './base';
import { defaultEventCenter } from '@app-base/common/events/DefaultEventCenter';

export default class CmdInteractor extends Base {
  setParam: any;
  setActive: any;
  formKey: string;
  points: { [key: string]: Vector3 } = {};
  name: string;
  constructor({ setParam, setActive }) {
    super();
    this.setParam = setParam;
    this.setActive = setActive;
    this.setMode(['face', 'point']);
    this.registerEvents();
  }
  setFormKey(key) {
    this.formKey = key;
  }
  setPointAndParam(point: Vector3, entityId?: string, name?: string) {
    this.point = point;
    this.entityId = entityId;
    if (name) {
      this.name = name;
    }
    this.setParam((state) => ({
      ...state,
      [this.formKey]: {
        entityId: this.entityId,
        point: this.point?.toArray().map((i) => parseFloat(i.toFixed(6))),
        name: name || this.getName(),
      },
    }));
  }
  removeByFormKey(key?: string | string[]) {
    if (Array.isArray(key)) {
      key.forEach((k) => {
        this.removeByFormKey(k);
      });
    } else {
      this.points[key || this.formKey] = null;
      this.renderPoint();
    }
  }
  resetSetting() {
    this.setting = {};
    this.containIds = null;
  }
  registerEvents = () => {
    defaultEventCenter()
      .on(CENTEREVENTS.POINTERMOVE, this.onMouseMove)
      .on(CENTEREVENTS.POINTERDOWN, this.onMouseUp)
      .addExit(this.destroy)
      .on(CENTEREVENTS.KEYUP, this.onKeyUp);
  };
  unregisterEvents = () => {
    defaultEventCenter()
      .off(CENTEREVENTS.POINTERMOVE, this.onMouseMove)
      .off(CENTEREVENTS.POINTERDOWN, this.onMouseUp)
      .off(CENTEREVENTS.KEYUP, this.onKeyUp)
      .setDefault();
  };
  setLimitPick(limitPick) {
    this.limitPick = limitPick;
    this.containIds = this.limitPick?.modelData?.faces[
      this.limitPick?.idInfo?.i
    ]?.edges.map((i) => this.limitPick?.idInfo?.o + ':e' + i);
    this.setting = {
      pickable: {
        dxids: [this.limitPick?.idInfo?.o],
        pickable: true,
      },
    };
    this.resetPoint();
  }
  onMouseMove = (e) => {
    if (this.limitPick || this.isPick) {
      this.mouseMove(e);
    }
  };
  onMouseUp = (e) => {
    if (e.button.onLeft && this.point) {
      if (this.pickInfo?.assisObj?.name === 'pickedFace') {
        this.picker.select(e);
      }
      if (this.limitPick || this.isPick) {
        this.setParam((state) => ({
          ...state,
          [this.formKey]: {
            entityId: this.entityId,
            point: this.point?.toArray().map((i) => parseFloat(i.toFixed(6))),
            name: this.getName(),
          },
        }));
        this.renderPoint();
      }
      // this.setActive((state) => (state < 2 ? state + 1 : state));
    }
  };
  resetPoint = () => {
    removePoint();
    this.points = {};
  };
  // 渲染点
  renderPoint = () => {
    removePoint();
    this.formKey && (this.points[this.formKey] = this.point);
    createPoint(Object.values(this.points).filter((item) => item != null));
    ctx2.viewer.requestRender();
  };
  // 获取name
  getName = () => {
    let name = '';
    if (this.entityType === 'point') {
      name = this.pickInfo.objInfo.label + '上的点' + this.pickInfo.idInfo?.i;
    } else if (this.entityType === 'line') {
      name =
        this.pickInfo.objInfo.label +
        '上的边' +
        this.pickInfo.idInfo?.i +
        '的点：';
    } else if (this.entityType === 'face') {
      name =
        this.pickInfo.objInfo.label +
        '上的面' +
        this.pickInfo.idInfo?.i +
        ' 的点：';
    }
    return name;
  };
  onEnter = () => {
    //
  };
  selfReset(): void {
    this.reset();
    this.resetPoint();
    this.setting = {};
  }
  destroy = () => {
    this.unregisterEvents();
    this.removePointArrow();
    this.reset();
  };
  addExit(exitFunc: () => void) {
    this.exitFunc = exitFunc;
  }
}
